<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      完全背包：
      物品任取，装满背包的最大价值：外层物品，内层背包容量，从小到大遍历 j从weight[i]开始
      dp[j]=Math.max(dp[j],dp[j-weight[i]]+value[i])

      求装满背包有几种组合方式：外层物品，内层背包容量，初始化dp=0 dp[0]=1
      dp[j]+=dp[j-weight[i]]

      装满背包的最小物品数 初始化infinity dp[0]=0
      dp[j]=Math.min(dp[j],dp[j-weight[i]]+1)

      排列 外层背包内层物品  if(j>=weight[i])
       */
      var numSquares = function (n) {
        //物品 1-n，重量 1到n^2 装满背包的最少物品数
        let bagSize = n
        let dp = new Array(bagSize + 1).fill(Infinity)
        dp[0] = 0
        for (let i = 1; i <= n; i++) {
          let weight = i * i
          for (let j = weight; j <= bagSize; j++) {
            dp[j] = Math.min(dp[j], dp[j - weight] + 1)
          }
        }
        return dp[bagSize] == Infinity ? 1 : dp[bagSize]
      }
      numSquares(12)
    </script>
  </body>
</html>
